//
//  GYEmoticonPageViewCell.swift
//  Weibo
//
//  Created by vane on 16/8/18.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class GYEmoticonPageViewCell: UICollectionViewCell {
    
    var emoticons:[GYEmoticonModel]?{
        
        didSet{
            
            for button in buttonsArray {
                button.hidden = true
            }
            
            for (i,emoticonModel) in emoticons!.enumerate() {
                
                let button = buttonsArray[i]
                
                button.hidden = false
                // 判断他是图片表情 还是emoji表情
                // 是emoji 表情 需要设置button 的title
                if emoticonModel.isEmoji {
                    let code = (emoticonModel.code ?? "" as NSString).emoji()
                    button.setTitle(code, forState: UIControlState.Normal)
                
                    button.setImage(nil, forState: UIControlState.Normal)
                    
                }else {
                    // 是图片表情 需要设置button 的image
                  
                    button.setImage(UIImage.emoticonsImage(emoticonModel), forState: UIControlState.Normal)
                   
                    button.setTitle(nil, forState: UIControlState.Normal)
                }
            }

            
        }
    }
    
    var indexPath: NSIndexPath?{
        didSet{
            
            messageLabel.text = "第\(indexPath?.section ?? 0)组\n第\(indexPath?.item ?? 0)页"
        }
    }
    
    var buttonsArray:[UIButton] = [UIButton]()

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
        
        addChildButtons()
        
        contentView.addSubview(messageLabel)

        messageLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(contentView)
        }
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = self.frame.width/CGFloat(GYEmoticonsMaxCol)
        
        let h = self.frame.height/CGFloat(GYEmoticonsMaxRow)
        
        for (index, button) in buttonsArray.enumerate() {

            let col = CGFloat(index%GYEmoticonsMaxCol)
            
            let row = CGFloat(index/GYEmoticonsMaxCol)
            
            button.frame = CGRect(x: col*w, y: row*h, width: w, height: h)
        }
        
        
    }
    func addChildButtons(){
     
        for _ in 0..<GYEmoticonsMaxCount{
            
            let btn = UIButton()
            
//            btn.backgroundColor = RandomColor()
            
            btn.titleLabel?.font = UIFont.systemFontOfSize(30)

            buttonsArray.append(btn)
            
            contentView.addSubview(btn)

            
        }
        
    }
    private lazy var messageLabel: UILabel = UILabel(textColor: UIColor.redColor(), textSize: 35, maxWidth: SCREENW)
    
}
